package com.weavernorth.retailExperienceAward.action;

import com.weavernorth.retailExperienceAward.utils.sendDataToSapUtils;
import com.weavernorth.retailExperienceAward.dto.sapMessEntity;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.List;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.retailExperienceAward.action
 * @date: 2020/6/15 18:08
 * @description: 增补/扣除零售体验奖数据传至SAP系统
 */
public class zbkcRetailAwardToSAP implements Action {
    private Logger log= Logger4jUtil.getLoggerByName("/action/zbkcRetailAwardToSAP/");
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs_main=new RecordSet();
        RecordSet rs_select=new RecordSet();
        RecordSet rsUpdate=new RecordSet();
        log.info("增补/扣除零售体验奖传数开始。。。");
        //请求id
        String strRequestid = requestInfo.getRequestid();
        log.info("流程requestid="+strRequestid);
        String strTable = requestInfo.getRequestManager().getBillTableName();
        log.info("表名："+strTable);
        String strKhzq="";
        //查询表单数据
        String strData="select * from "+strTable+" m where  m.requestid=?";
        rs_main.executeQuery(strData);
        String strType="";//获取操作类别
        while (rs_main.next()){
            strType=Util.null2String(rs_main.getString("czlb"));
        }
        String strSqlDetail="";
        if (strType=="0"){//扣减
            strSqlDetail+="select m.*,n.* from "+strTable+" m,"+strTable+"_dt1 n where m.id=n.MAINID and m.requestid=?";
        }else if (strType=="1"){//增补
            strSqlDetail+="select m.*,n.* from "+strTable+" m,"+strTable+"_dt2 n where m.id=n.MAINID and m.requestid=?";
        }
        rs_select.executeQuery(strSqlDetail);
        //报文请求体
        StringBuffer requestXml;
        requestXml = new StringBuffer();
        //拼接请求体
        requestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:rfc:functions'>" +
                "<soapenv:Header/>" + "<soapenv:Body>" + "<urn:ZHRI_075>" + " <RETURN>" +
                "<!--Zero or more repetitions:-->" +
                "<item>" +
                "<TYPE></TYPE>" +
                "<ID></ID>" +
                "<NUMBER></NUMBER>" +
                "<MESSAGE></MESSAGE>" +
                "<LOG_NO></LOG_NO>" +
                "<LOG_MSG_NO></LOG_MSG_NO>" +
                "<MESSAGE_V1></MESSAGE_V1>" +
                "<MESSAGE_V2></MESSAGE_V2>" +
                "<MESSAGE_V3></MESSAGE_V3>" +
                "<MESSAGE_V4></MESSAGE_V4>" +
                "<PARAMETER></PARAMETER>" +
                "<ROW></ROW>" +
                "<FIELD></FIELD>" +
                "<SYSTEM></SYSTEM>" +
                "</item>" +
                "</RETURN>" +
                "<T_9133>" +
                "<!--Zero or more repetitions:-->");
        while (rs_select.next()){
            strKhzq= Util.null2String(rs_select.getString("khzq"));
            //获取员工编号
            String strYgbh = Util.null2String(rs_select.getString("gh"));
            log.info("员工编号：" + strYgbh);
            //拼接XML
            requestXml.append(" <item>" + "<PERNR>" + strYgbh + "</PERNR>");
            //获取CRM评分
            String strCRM = Util.null2String(rs_select.getString("crm5"));
            log.info("CRM评分：" + strCRM);
            //拼接XML
            requestXml.append("<ZCRMPF>" + strCRM + "</ZCRMPF>");
            //获取零售运营评分
            String strLsyy = Util.null2String(rs_select.getString("yybz35"));
            log.info("零售运营评分：" + strLsyy);
            //拼接XML
            requestXml.append("<ZLSYYPF>" + strLsyy + "</ZLSYYPF>");
            //获取服务标准评分
            String strFwbz = Util.null2String(rs_select.getString("fwbz40"));
            log.info("服务标准评分：" + strFwbz);
            //拼接XML
            requestXml.append("<ZFWBZPF>" + strFwbz + "</ZFWBZPF>");
            //获取陈列管理
            String strClgl = Util.null2String(rs_select.getString("clbz20"));
            log.info("陈列管理评分：" + strClgl);
            //拼接XML
            requestXml.append("<ZCLGLPF>" + strClgl + "</ZCLGLPF>");
            //总得分
            String strZdf = Util.null2String(rs_select.getString("zdf"));
            log.info("总得分：" + strZdf);
            //拼接XML
            requestXml.append("<ZDF>" + strZdf + "</ZDF>");
            //评分日期
            String strPfrq = Util.null2String(rs_select.getString("khrq"));
            log.info("评分日期：" + strPfrq);
            log.info("传至SAP日期：" + dateUtils.getLastDate(strPfrq));
            //拼接XML
            requestXml.append("<ZSP_DATE>" + dateUtils.getLastDate(strPfrq) + "</ZSP_DATE>");
            //姓名
            String strXm=Util.null2String(rs_select.getString("xm"));
            String strGzfw= sendDataToSapUtils.getGzfwFromUser(strXm);
            String strLockMess = sendDataToSapUtils.judgeLock(log,strGzfw, strPfrq);
            log.info("工资核算范围返回信息:" + strLockMess);
            if (!"".equals(strLockMess)) {
                strLockMess="工号为:"+strYgbh+","+strLockMess+"薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                log.info(strLockMess);
            }
            //奖金金额
            String strJjje="";
            if (strType=="0"){
                strJjje=Util.null2String(rs_select.getString("dzjjje"));
            }else if (strType=="1"){
                strJjje=Util.null2String(rs_select.getString("bfjjje"));
            }
            log.info("奖金金额：" + strJjje);
            //拼接XML
            requestXml.append("<ZJE>" + strJjje + "</ZJE>" + "</item>");
        }
        requestXml.append("</T_9133>" +
                "</urn:ZHRI_075>" +
                "</soapenv:Body>" +
                "</soapenv:Envelope>");
        List<sapMessEntity> list = sendDataToSapUtils.toSapMethod(log,requestXml.toString());
        String strAllFlag="";
        String strMessage="";
        for (sapMessEntity entity : list) {
            if (entity.getType().equals("allS")){
                strAllFlag="allS";//全部成功
                strMessage=entity.getMessage();
            }else if (entity.getType().equals("allE")){
                strAllFlag="allE";//全部失败
                strMessage=entity.getMessage();
            }else{
                strAllFlag="E";//部分成功部分失败
            }
        }
        if (list.size()<1) {
            log.info("接口信息解析异常！");
            requestInfo.getRequestManager().setMessagecontent("接口信息解析异常!");
            return FAILURE_AND_CONTINUE;
        }else if ("allE".equals(strAllFlag)){//全部失败
            log.info("SAP接口传数失败,失败原因："+strMessage);
            requestInfo.getRequestManager().setMessagecontent(strMessage);
            return FAILURE_AND_CONTINUE;
        }else if ("allS".equals(strAllFlag)){//全部成功
            String strUpdateSql = "update "+strTable+" set sapfhjg =? where requestid= ?";
            rsUpdate.executeQuery(strUpdateSql,strMessage,strRequestid);
        }else{//部分成功部分失败
            for (sapMessEntity entity : list) {
                if (!entity.getType().equals("allS")&&!entity.getType().equals("allE")){
                    if (entity.getType().equals("E")){
                        strMessage+=entity.getMessage();
                    }
                }
            }
            String strUpdateSql = "update "+strTable+" set sapfhjg =? where requestid= ?";
            rsUpdate.executeQuery(strUpdateSql,strMessage,strRequestid);
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }

}
